package com.csot.ry.controller;

import com.csot.ry.domain.TCityIp;
import com.csot.ry.domain.TDetectJob;
import com.csot.ry.service.ITCityIpService;
import com.csot.ry.service.ITDetectJobService;
import com.csot.common.annotation.Log;
import com.csot.common.core.controller.BaseController;
import com.csot.common.core.domain.AjaxResult;
import com.csot.common.core.page.TableDataInfo;
import com.csot.common.enums.BusinessType;
import com.csot.common.utils.poi.ExcelUtil;
import com.jfinal.plugin.activerecord.Db;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 探活Controller
 *
 * @author 刘沙
 * @date 2022-06-15
 */
@RestController
@RequestMapping("/ipmanagement/detect")
public class TDetectJobController extends BaseController {
    @Autowired
    private ITDetectJobService tDetectJobService;

    @Autowired
    private ITCityIpService itCityIpService;

    /**
     * 查询探活列表
     */
    @PreAuthorize("@ss.hasPermi('ipmanagement:detect:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDetectJob tDetectJob) {
        startPage();
        List<TDetectJob> list = tDetectJobService.selectTDetectJobList(tDetectJob);
        return getDataTable(list);
    }

    /**
     * 导出探活列表
     */
    @PreAuthorize("@ss.hasPermi('ipmanagement:detect:export')")
    @Log(title = "探活", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TDetectJob tDetectJob) {
        List<TDetectJob> list = tDetectJobService.selectTDetectJobList(tDetectJob);
        ExcelUtil<TDetectJob> util = new ExcelUtil<TDetectJob>(TDetectJob.class);
        util.exportExcel(response, list, "探活数据");
    }

    /**
     * 获取探活详细信息
     */
    @PreAuthorize("@ss.hasPermi('ipmanagement:detect:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {

        TDetectJob tdj = tDetectJobService.selectTDetectJobById(id);
        TDetectJobDetect tDetectJobDetect = new TDetectJobDetect();
        BeanUtils.copyProperties(tdj, tDetectJobDetect);
        long shizhanipcount = Db.queryLong("select count(id) from t_city_ip where ip_job_id = ? and address_status = 'sz' ", tdj.getId());
        long kongxianipcount = Db.queryLong("select count(id) from t_city_ip where ip_job_id = ? and address_status = 'kx' ", tdj.getId());
        tDetectJobDetect.setKongxianipcount(kongxianipcount);
        tDetectJobDetect.setShizhanipcount(shizhanipcount);
        return AjaxResult.success(tDetectJobDetect);
    }

    @PreAuthorize("@ss.hasPermi('ipmanagement:detect:list')")
    @GetMapping("/shizhanip/{id}")
    public TableDataInfo shizhanip(@PathVariable("id") Long id) {
        startPage();
        TDetectJob tdj = tDetectJobService.selectTDetectJobById(id);
        TCityIp tCityIp = new TCityIp();
        tCityIp.setIpJobId(tdj.getId());
        tCityIp.setAddressStatus("sz");
        return getDataTable(itCityIpService.selectTCityIpList(tCityIp));
    }

    @PreAuthorize("@ss.hasPermi('ipmanagement:detect:list')")
    @GetMapping("/kongxianip/{id}")
    public TableDataInfo kongxianip(@PathVariable("id") Long id) {
        startPage();
        TDetectJob tdj = tDetectJobService.selectTDetectJobById(id);
        TCityIp tCityIp = new TCityIp();
        tCityIp.setIpJobId(tdj.getId());
        tCityIp.setAddressStatus("kx");
        return getDataTable(itCityIpService.selectTCityIpList(tCityIp));
    }

    /**
     * 新增探活
     */
    @PreAuthorize("@ss.hasPermi('ipmanagement:detect:add')")
    @Log(title = "探活", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDetectJob tDetectJob) {
        return toAjax(tDetectJobService.insertTDetectJob(tDetectJob));
    }

    /**
     * 修改探活
     */
    @PreAuthorize("@ss.hasPermi('ipmanagement:detect:edit')")
    @Log(title = "探活", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDetectJob tDetectJob) {
        return toAjax(tDetectJobService.updateTDetectJob(tDetectJob));
    }

    /**
     * 删除探活
     */
    @PreAuthorize("@ss.hasPermi('ipmanagement:detect:remove')")
    @Log(title = "探活", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        int i = tDetectJobService.stopTDetectJobById(ids[0]);
        return toAjax(i);
    }

    class TDetectJobDetect extends TDetectJob {
        long shizhanipcount = 0;

        long kongxianipcount = 0;

        public long getShizhanipcount() {
            return shizhanipcount;
        }

        public void setShizhanipcount(long shizhanipcount) {
            this.shizhanipcount = shizhanipcount;
        }

        public long getKongxianipcount() {
            return kongxianipcount;
        }

        public void setKongxianipcount(long kongxianipcount) {
            this.kongxianipcount = kongxianipcount;
        }
    }


}
